/**
 * Beaver - Zend/Ext.Direct framework
 *
 * @license     http://www.gnu.org/licenses/gpl-2.0.txt
 * @author      Mihály Kuprivecz <mkuprivecz@gmail.com>
 * @namespace   Beaver.Modules.User
 */

Ext.ns('Beaver.Modules.Country');

Beaver.Modules.Country.Store = Ext.extend( Beaver.Modules.General.Store, {
	constructor: function(config) {
		config        = config || {};
		config.fields = config.fields || [];
		config.controllerName = 'Country';
		Ext.applyIf( config.fields, [
			{ name: 'id', mapping: 'id' },
			{ name: 'name_hu', mapping: 'name_hu', allowBlank: false },
			{ name: 'name_en', mapping: 'name_en', allowBlank: false }
		]);
		
		Beaver.Modules.Country.Store.superclass.constructor.apply(this, [config]);
	}
});

Ext.reg('beavercountry', Beaver.Modules.Country.Store);


Beaver.Modules.Country.Grid = Ext.extend(Beaver.Modules.General.Grid, {
	initComponent: function() {
		var store  = new Beaver.Modules.Country.Store();
		var editor = new Beaver.Modules.General.RowEditor();
		//console.log(store);
		editor.on('canceledit', function() {
			if(store.getAt(0).phantom)
			{
				store.removeAt(0);
			}
		});
		
		var self  = this;
		var config = {
			store: store,
			plugins: [ editor ],
			columns: [
				{
					header: 'Kód',
					width: 20,
					sortable: true,
					dataIndex: 'id',
					editor: new Ext.form.TextField()
				},
				{
					header: 'Név (magyar)',
					sortable: true,
					dataIndex: 'name_hu',
					editor: new Ext.form.TextField()
				},
				{
					header: 'Név (angol)',
					sortable: true,
					dataIndex: 'name_en',
					editor: new Ext.form.TextField()
				}

			],
			tbar: [
				{
					text: 'Ország hozzáadása',
					handler: function(){

						if(self.store.getAt(0) && self.store.getAt(0).phantom)
						{
							self.store.removeAt(0);
						}
						var record = new store.recordType({
							code: '',
							name_hu : '',
							name_en : ''
						});

						editor.stopEditing();
						self.store.insert(0, record);
						editor.startEditing(0);
					}
				},
				{
					text: 'Kijelöltek törlése',
					handler: function(){
						
						editor.stopEditing();
						var records = self.getSelectionModel().getSelections();
						if (records.length > 0)
						{
					
							Ext.Msg.confirm('Törélés', 'Biztosan törölni szeretné az összes kijelölt elemet?', function( answer ){
								if (answer == "yes")
								{
									self.getStore().remove(records);
								}
				        	},this);
						}
					}
				}
			]
		};
		Ext.apply(this, config);
		Ext.apply(this.initialConfig, config);
		Beaver.Modules.Country.Grid.superclass.initComponent.apply(this, arguments);
	},
	onRender:function() {
		Beaver.Modules.Country.Grid.superclass.onRender.apply(this, arguments);
	}
});

Ext.reg('beavergridcountry', Beaver.Modules.Country.Grid);